package com.itheima.imclient100.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.itheima.imclient100.BaseActivity;
import com.itheima.imclient100.R;
import com.itheima.imclient100.presenter.RegistPresenter;
import com.itheima.imclient100.presenter.impl.RegistPresenterImpl;
import com.itheima.imclient100.utils.SPUtils;
import com.itheima.imclient100.utils.Utils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by fullcircle on 2017/7/2.
 */

public class RegistActivity extends BaseActivity implements RegistView{
    @InjectView(R.id.iv_avatar)
    ImageView ivAvatar;
    @InjectView(R.id.et_username)
    EditText etUsername;
    @InjectView(R.id.til_username)
    TextInputLayout tilUsername;
    @InjectView(R.id.et_pwd)
    EditText etPwd;
    @InjectView(R.id.til_pwd)
    TextInputLayout tilPwd;
    @InjectView(R.id.btn_regist)
    Button btnRegist;

    private RegistPresenter mPresenter;
    private String username;
    private String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.inject(this);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        mPresenter = new RegistPresenterImpl(this);
    }

    @OnClick(R.id.btn_regist)
    public void onClick() {
        //用户名密码的校验 用户名是字符和数字
        username = etUsername.getText().toString().trim();
        pwd = etPwd.getText().toString().trim();
        // mPresenter.regist();
        if(!Utils.checkUsername(username)){
            //设置可以显示错误信息的布局
            tilUsername.setErrorEnabled(true);
            tilUsername.setError("用户名不合法");
            return;
        }else{
            //设置关闭显示错误信息的布局
            tilUsername.setErrorEnabled(false);
        }


        if(!Utils.checkPassword(pwd)){
            tilPwd.setErrorEnabled(true);
            tilPwd.setError("密码不合法");
            return;
        }else{
            tilPwd.setErrorEnabled(false);
        }

        System.out.println(username + pwd);
        mPresenter.regist(username, pwd);
        showProgressDialog("正在注册....");
    }

    @Override
    public void onGetRegistResult(boolean isSuccess, String errMsg) {
        cancelProgressDialog();
        if(isSuccess){
           //返回登录页面
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
//            intent.putExtra("username",username);
//            intent.putExtra("pwd",pwd);
            SPUtils.savePassword(this,pwd);
            SPUtils.saveUsername(this,username);
            startActivity(intent);

            finish();
        }else{
          // 弹出toast
            showToast(errMsg);
        }
    }
}
